package com.zuihai.app.mvp.presenter;

import android.app.Application;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.blankj.utilcode.util.LogUtils;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.mvp.IView;
import com.jess.arms.utils.RxUtils;
import com.zuihai.app.BuildConfig;
import com.zuihai.app.mvp.contract.DetailContract;
import com.zuihai.app.mvp.model.entity.BaseJson;
import com.zuihai.app.mvp.model.entity.Detail;
import com.zuihai.app.mvp.model.entity.Detail.Comment;
import com.zuihai.app.mvp.model.entity.Detail.Label;
import com.zuihai.app.mvp.model.entity.Detail.Recommend;
import com.zuihai.app.mvp.model.entity.MessageResponse;
import com.zuihai.app.mvp.ui.adapter.DetailLabelAdapter;
import com.zuihai.app.mvp.ui.utils.AccountHelper;
import com.zuihai.app.mvp.ui.utils.MyUtils;
import com.zuihai.app.mvp.ui.widget.MyToast;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/5/12 0012.
 */

@FragmentScope
public class DetailPresenter extends BasePresenter<DetailContract.Model, DetailContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private AppManager mAppManager;
    private List<Recommend> mRecommends = new ArrayList();
    private List<Comment> mComments = new ArrayList();
    private List<Label> mLabels = new ArrayList<>();

    private DetailLabelAdapter mLabelAdapter;
    private int mPage = 1;
    private String mEmpty;

    private String jsonMsg;
    private String jsonCode;


    @Inject
    public DetailPresenter(DetailContract.Model model, DetailContract.View rootView
            , RxErrorHandler handler, Application application, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mAppManager = appManager;
        init();
    }

    private void init() {



        mLabelAdapter = new DetailLabelAdapter(mLabels);
        mRootView.bindLabelRecycler(mLabelAdapter);
    }


    public void requestService(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestService(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBase, BaseJson<Comment> commentBase) {
                        mEmpty=detailBase.msg;
                        if (detailBase.isSuccess() && commentBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            detail.comments = commentBase.data.result;
                            return detail;
                        } else if (detailBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            return detail;
                        }else if (detailBase.isDelete()){
                            LogUtils.e("空的："+detailBase.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    public void requestGoodGoods(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestGoodGoods(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBase, BaseJson<Comment> commentBase) {
                        mEmpty=detailBase.msg;
                        if (detailBase.isSuccess() && commentBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            detail.comments = commentBase.data.result;
                            return detail;
                        } else if (detailBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            return detail;
                        }else if (detailBase.isDelete()){
                            LogUtils.e("空的："+detailBase.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 酒城日记详情
     *
     * @param id
     */
    public void requestDiary(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestDiary(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求今日头条详情
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requestHeadline(String id) {
        LogUtils.e("头条详情");
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestHeadline(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            mRootView.adContent(detailBaseJson.detailAd);
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 推荐之后 自动跳转推荐
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requestHeadlineIntentTuijian(String id) {
        LogUtils.e("头条详情");
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestHeadline(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            mRootView.adContent(detailBaseJson.detailAd);
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriberIntentTuijian());
    }

    /**
     * 请求热点详情
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requestHot(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestHot(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;

                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            mEmpty=detailBaseJson.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求逛街详情
     */
    public void requestShopping(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShopping(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求周边详情
     */
    public void requestPeryphery(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestPeryphery(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求活动详情
     */
    public void requestActivity(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestActivity(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());

                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求互助详情
     */
    public void requestHelp(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestHelp(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求故事详情
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requestStory(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestStory(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;

                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            mEmpty=detailBaseJson.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求人物详情
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requesChara(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestCharacter(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;

                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            mEmpty=detailBaseJson.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }



    /**
     * 请求视频详情
     * 包括第一页评论列表
     *
     * @param id 文章ID
     */
    public void requestVideo(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestVideo(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {
                        mEmpty=detailBaseJson.msg;

                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            mEmpty=detailBaseJson.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }


    /**
     * 发现秘密
     *
     * @param id
     */
    public void requestSecret(String id) {
        String uid = AccountHelper.getInstance().getUid();
        LogUtils.e("发现秘密详情   id" + id + "uid :" + uid);
        mModel.requestSecret(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBaseJson, BaseJson<Comment> baseJson) {

                        mEmpty=detailBaseJson.msg;
                        if (detailBaseJson.isSuccess() && baseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            detail.comments = baseJson.data.result;
                            return detail;
                        } else if (detailBaseJson.isSuccess()) {
                            Detail detail = detailBaseJson.data.result.get(0);
                            return detail;
                        }else if (detailBaseJson.isDelete()){
                            LogUtils.e("空的："+detailBaseJson.isDelete());
                            mEmpty=detailBaseJson.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求店铺详情(有去处)
     *
     * @param id 店铺Id
     */
    public void requestShop(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShop(id, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBase, BaseJson<Comment> commentBase) {
                        mEmpty=detailBase.msg;

                        if (detailBase.isSuccess() && commentBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            detail.comments = commentBase.data.result;
                            return detail;
                        } else if (detailBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            return detail;
                        }else if (detailBase.isDelete()){
                            LogUtils.e("空的："+detailBase.isDelete());
                            mEmpty=detailBase.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 请求商品/服务详情
     *
     * @param id 店铺Id
     */
    public void requestCommodity(String id, int commodityType) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestCommodity(id, commodityType, uid)
                .zipWith(mModel.requestAddComment(id, mPage, uid), new Func2<BaseJson<Detail>, BaseJson<Comment>, Detail>() {
                    @Override
                    public Detail call(BaseJson<Detail> detailBase, BaseJson<Comment> commentBase) {
                        mEmpty=detailBase.msg;
                        if (detailBase.isSuccess() && commentBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            detail.comments = commentBase.data.result;
                            return detail;
                        } else if (detailBase.isSuccess()) {
                            Detail detail = detailBase.data.result.get(0);
                            return detail;
                        }else if (detailBase.isDelete()){
                            LogUtils.e("空的："+detailBase.isDelete());
                            mEmpty=detailBase.msg;
                            return null;
                        }
                        return null;
                    }
                })
                .compose(detailSchedulers(mRootView))
                .subscribe(detailSubscriber());
    }

    /**
     * 收藏头条
     *
     * @param id
     * @param isCollect
     */
    public void headlineCollect(String id, String type, boolean isCollect, String releaser_id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.headlineCollect(id, uid, type, isCollect, releaser_id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if ("我很喜欢".equals(response.data)||"收藏成功".equals(response.data)) {
                           MyToast.showSuccess(response.data);
                        } else {
                            MyToast.showError(response.data);
                        }
                    }
                });
    }

    /**
     * 收藏发现秘密
     *
     * @param id
     * @param isCollect
     */
    public void secretCollect(String id, boolean isCollect) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.secretCollect(id, uid, isCollect)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            String data = response.data;
                            mRootView.showMessage(data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 收藏有去处
     *
     * @param id
     * @param isCollect
     */
    public void shopCollect(String id, boolean isCollect) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.shopCollect(id, uid, isCollect)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            String data = response.data;
                            mRootView.showMessage(data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 收藏物品
     *
     * @param id
     * @param isCollect
     */
    public void commodityCollect(String id, boolean isCollect) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.commodityCollect(id, uid, isCollect)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            String data = response.data;
                            mRootView.showMessage(data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 收藏服务
     *
     * @param id
     * @param isCollect
     */
    public void serverCollect(String id, boolean isCollect) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.serverCollect(id, uid, isCollect)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            String data = response.data;
                            mRootView.showMessage(data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 请求评论列表
     *
     * @param id 文章ID
     */
    public void requestComment(String id) {
        String uid = AccountHelper.getInstance().getUid();
        LogUtils.e("请求评论了");
        mModel.requestAddComment(id, mPage, uid)
                .compose(commentSchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Comment>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Comment> comments) {
                        if (comments != null) {

                        } else {
                            mRootView.showMessage("评论列表加载失败");

                        }
                        if (MyUtils.isScroll){
                            LogUtils.e("onPinglunCommentList();");
                            mRootView.scrollComment();
                        }else {
                            LogUtils.e("onPinglunCommentList(); xxxxxxxxxxx");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mRootView.showMessage("评论列表加载失败");

                    }
                });
    }



    /**
     * 关注用户
     *
     * @param id
     * @param isCare
     */
    public void requestCare(String id, int isCare) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestCare(id, uid, isCare)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showFocusMessage(response.data);
                        } else {
                            mRootView.showFocusMessage(response.data);
                        }
                    }
                });
    }

    /**
     * 评论点赞
     *
     * @param id
     * @param isFavour
     */
    public void commentFavour(String id, int isFavour, String releaserId,String type) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.commentFavour(id, uid, isFavour, releaserId,type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                           // mRootView.showFavourMessage(response.data);
                        } else {
                           // mRootView.showFavourMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 头条详情点赞  注释掉点赞红包的请求
     *
     * @param id
     */
    public void handlineFavour(String id, String releaserId,String type) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.handlineFavour(id, uid, releaserId,"1",type)
                /*.zipWith(mModel.requestFavourLuckyMoney(id, uid), new Func2<MessageResponse, MessageResponse, MessageResponse>() {
                    @Override
                    public MessageResponse call(MessageResponse response, MessageResponse response2) {
                        return response2;
                    }
                })*/
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.data);
                        }
                    }
                });
    }

    /**
     * 发现秘密详情点赞
     *
     * @param id
     */
    public void secretFavour(String id, String releaserId,String type) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.secretFavour(id, uid, releaserId,"1",type)
                .zipWith(mModel.requestFavourLuckyMoney(id, uid), new Func2<MessageResponse, MessageResponse, MessageResponse>() {
                    @Override
                    public MessageResponse call(MessageResponse response, MessageResponse response2) {
                        return response2;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 有去处详情点赞
     *
     * @param id
     */
    public void shopFavour(String id, String releaserId) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.shopFavour(id, uid, releaserId)
                .zipWith(mModel.requestFavourLuckyMoney(id, uid), new Func2<MessageResponse, MessageResponse, MessageResponse>() {
                    @Override
                    public MessageResponse call(MessageResponse response, MessageResponse response2) {
                        return response2;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 服务详情点赞
     *
     * @param id
     */
    public void serverFavour(String id, String releaserId) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.serverFavour(id, uid, releaserId)
                .zipWith(mModel.requestFavourLuckyMoney(id, uid), new Func2<MessageResponse, MessageResponse, MessageResponse>() {
                    @Override
                    public MessageResponse call(MessageResponse response, MessageResponse response2) {
                        return response2;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 商品详情点赞
     *
     * @param id
     */
    public void commodityFavour(String id, String releaserId) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.commodityFavour(id, uid, releaserId)
                .zipWith(mModel.requestFavourLuckyMoney(id, uid), new Func2<MessageResponse, MessageResponse, MessageResponse>() {
                    @Override
                    public MessageResponse call(MessageResponse response, MessageResponse response2) {
                        return response2;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showMessage(response.data);
                        } else {
                            mRootView.showMessage(response.msg);
                        }
                    }
                });
    }

    /**
     * 添加评论
     *
     * @param id
     * @param comment
     */
    public void requestAddComment(String id, String comment, String release, String picture,String type) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestAddComment(id, uid, comment, release, picture,type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Comment>, Comment>() {
                    @Override
                    public Comment call(BaseJson<Comment> commentBaseJson) {
                        if (commentBaseJson.isSuccess()) {
                            return commentBaseJson.data.result.get(0);
                        }
                        mRootView.showErrorMessage(commentBaseJson.msg);
                        return null;
                    }
                })
                .compose(RxUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mRootView.showReplyLoading();
                    }
                })
                .doOnCompleted(new Action0() {
                    @Override
                    public void call() {
                        mRootView.hideReplyLoading();
                    }
                })
                .subscribe(new ErrorHandleSubscriber<Comment>(mErrorHandler) {
                    @Override
                    public void onNext(Comment comment) {
                        LogUtils.e("看看到这里了马");
                        if (comment!=null){
                            mRootView.visibleFramelayout();
                        }
                    }
                });
    }


    /**
     * 请求评论列表
     *
     * @param id 文章ID
     */
    public void requestComment(String id,boolean isFirst) {
        String uid = AccountHelper.getInstance().getUid();
        LogUtils.e("请求评论了");
        mModel.requestAddComment(id, mPage, uid)
                .compose(commentSchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<List<Comment>>(mErrorHandler) {
                    @Override
                    public void onNext(List<Comment> comments) {
                        if (comments != null) {


                        } else {

                        }
                        if (MyUtils.isScroll){
                            LogUtils.e("onPinglunCommentList();");
                            mRootView.scrollComment();
                        }else {
                            LogUtils.e("onPinglunCommentList(); xxxxxxxxxxx");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);

                    }
                });
    }

    /**
     * 新接口举报
     *
     * @param id
     * @param inform
     */
    public void requestInform(String id, String inform, String type) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestInform(id, uid, inform, type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (BuildConfig.DEBUG) {
                            LogUtils.e(response.toString());
                        }
                        if (response.isSuccess()) {
                            mRootView.showToast(response.data);
                        } else {
                            mRootView.showToast(response.data);
                        }
                    }
                });
    }

    /**
     * 首页 头条文章/酒城日记详情分享红包
     *
     * @param id
     */
    public void homeShareLuckyMoney(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShareLuckyMoney(id, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showShareMessage(response.data);
                        } else {
                            // TODO 分享红包失败
                        }
                    }
                });
    }

    /**
     * 发现秘密详情分享红包
     *
     * @param id
     */
    public void secretShareLuckyMoney(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShareLuckyMoney(id, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showShareMessage(response.data);
                        } else {
                            // TODO 分享红包失败
                        }
                    }
                });
    }

    /**
     * 有去处详情分享红包
     *
     * @param id
     */
    public void shopShareLuckyMoney(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShareLuckyMoney(id, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showShareMessage(response.data);
                        } else {
                            // TODO 分享红包失败
                        }
                    }
                });
    }

    /**
     * 逛街详情分享红包
     *
     * @param id
     */
    public void commodityShareLuckyMoney(String id) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.requestShareLuckyMoney(id, uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.showShareMessage(response.data);
                        } else {
                            // TODO 分享红包失败
                        }
                    }
                });
    }

    /**
     * 删除评论
     *
     * @param id
     */
    public void deleteComment(String id, int position) {
        mModel.deleteComment(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            MyToast.showSuccess(response.data);
                            //mRootView.showDeleteMessage(response.data);

                            mRootView.judgeEmptyView();
                        } else {
                            MyToast.showSuccess(response.data);
                            // TODO 分享红包失败
                        }
                    }
                });
    }

    @NonNull
    private ErrorHandleSubscriber<Detail> detailSubscriber() {
        return new ErrorHandleSubscriber<Detail>(mErrorHandler) {
            @Override
            public void onNext(Detail detail) {
                if (detail == null) {
                    LogUtils.e("detail == null");
                    EventBus.getDefault().post("" , "event_messageDelete");
                    mRootView.showEmpty(mEmpty+"");
                    return;
                }
                List<Label> labels = detail.label;
                if (!labels.isEmpty()) {
                    mLabelAdapter.getData().clear();
                    mLabelAdapter.addData(labels);
                }
                if (!TextUtils.isEmpty(detail.adUrl)) {
                    mRootView.fillAd(detail.adUrl);
                }
                List<Recommend> recommends = detail.recommends;

                List<Comment> comments = detail.comments;

                mRootView.fillContent(detail);
                mRootView.hideLoading();

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRootView.showError(e);//交给界面处理
            }
        };
    }

    @NonNull
    private ErrorHandleSubscriber<Detail> detailSubscriberIntentTuijian() {
        return new ErrorHandleSubscriber<Detail>(mErrorHandler) {
            @Override
            public void onNext(Detail detail) {
                if (detail == null) {
                    mRootView.showEmpty(mEmpty+"");
                    return;
                }
                List<Label> labels = detail.label;
                if (!labels.isEmpty()) {
                    mLabelAdapter.getData().clear();
                    mLabelAdapter.addData(labels);
                }
                if (!TextUtils.isEmpty(detail.adUrl)) {
                    mRootView.fillAd(detail.adUrl);
                }
                List<Recommend> recommends = detail.recommends;

                List<Comment> comments = detail.comments;

                mRootView.fillContent(detail);
                mRootView.hideLoading();
                mRootView.intentTuijian();

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mRootView.showError(e);//交给界面处理
            }
        };
    }

    public <T> Observable.Transformer<T, T> detailSchedulers(IView view) {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> observable) {
                return observable
                        .retryWhen(new RetryWithDelay(3, 3))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .doOnSubscribe(new Action0() {
                            @Override
                            public void call() {
                                mRootView.showLoading();
                            }
                        })
                        .doOnError(new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mRootView.showError(throwable);
                            }
                        })
                        .compose(RxUtils.bindToLifecycle(mRootView));
            }
        };
    }

    public <T> Observable.Transformer<BaseJson<T>, List<T>> commentSchedulers(IView view) {
        return observable -> observable
                .retryWhen(new RetryWithDelay(3, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(baseJson -> {
                    if (baseJson.isSuccess()) {
                        return baseJson.data.result;
                    }
                    return null;
                })
                .doAfterTerminate(new Action0() {
                    @Override
                    public void call() {
                        view.hideLoading();
                    }
                });
    }

    public void replyCommtent(String releaserId, String id,
                              String content, String beihuifu,
                              String picture,int position,
                              Detail.Comment mComment) {
        String uid = AccountHelper.getInstance().getUid();
        mModel.replyComment(releaserId, id, uid, content, beihuifu, picture)
                .retryWhen(new RetryWithDelay(3, 2))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<BaseJson<Detail.ReplyComment>, Detail.ReplyComment>() {
                    @Override
                    public Detail.ReplyComment call(BaseJson<Detail.ReplyComment> commentBaseJson) {
                        if (commentBaseJson.isSuccess()) {
                            return commentBaseJson.data.result.get(0);
                        }
                        jsonCode=commentBaseJson.code;
                        jsonMsg=commentBaseJson.msg;
                        return null;
                    }
                })
                .compose(RxUtils.bindToLifecycle(mRootView))
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mRootView.showReplyLoading();
                    }
                })
                .doOnCompleted(new Action0() {
                    @Override
                    public void call() {
                        mRootView.hideReplyLoading();
                    }
                })
                .subscribe(new ErrorHandleSubscriber<Detail.ReplyComment>(mErrorHandler) {
                    @Override
                    public void onNext(Detail.ReplyComment comment) {
                        mRootView.hideReplyLoading();
                    }
                });
    }


    /**
     * 是否有评论
     *
     * @param
     */
    public void haveComment(Detail.Comment mdata, int position) {
        mModel.haveComment(mdata.id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ErrorHandleSubscriber<MessageResponse>(mErrorHandler) {
                    @Override
                    public void onNext(MessageResponse response) {
                        if (response.isSuccess()) {
                            mRootView.haveComment(mdata,position);
                        } else {

                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mApplication = null;
    }
}
